package com.gxa.modules.service.impl;

import com.gxa.common.vo.Q;
import com.gxa.common.vo.ResultVo;
import com.gxa.modules.mapper.LsFinanceMapper;
import com.gxa.modules.pojo.*;
import com.gxa.modules.service.LsFinanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: zdk
 * Date: 2021/12/29 17:51
 * Description:
 * Version: V1.0
 */
@Service
public class LsFinanceServiceImpl implements LsFinanceService {
    @Autowired(required = false)
    LsFinanceMapper lsFinanceMapper;

    /**
     * 新增财务订单信息
     * @param forders 财务订单实体
     * @return ResultVo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVo addForders(Forders forders) {
        forders.setOr_pay(forders.getOr_amount().subtract(forders.getOr_discount()));
        forders.getFinance().setFinance_money(forders.getOr_pay());
        lsFinanceMapper.addForders(forders);
        Integer result = lsFinanceMapper.addFordersFinance(forders);
        return ruseltRsesultVo(result);
    }

    /**
     * 新增财务用户信息
     * @param financeUser 财务用户实体
     * @return ResultVo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVo addFinanceUser(Finance_User financeUser) {
        financeUser.getFinance().setFinance_money(financeUser.getFinance_user_money());
        lsFinanceMapper.addFinanceUser(financeUser);
        Integer result=lsFinanceMapper.addFinanceFinanceUser(financeUser);
       return ruseltRsesultVo(result);
    }

    /**
     *财务工资发放记录
     * @param todayDate 财务日结时间
     * @return ResultVo
     */
    @Override
    public ResultVo addFinancePayment(Date todayDate) {
        List<Finance> financeList=lsFinanceMapper.queryFinanceList(todayDate);
        Integer result=0;
        if(financeList.size()>0 && !financeList.isEmpty()){
            for(int i = 0;i<financeList.size();i++){
                java.util.Date datems=financeList.get(i).getFinance_date();
                Calendar calendar=new GregorianCalendar();
                calendar.setTime(datems);
                calendar.add(calendar.DATE,1);
                financeList.get(i).setFinance_date(calendar.getTime());
                result = lsFinanceMapper.addFinancePayment(financeList.get(i));
            }
            return ruseltRsesultVo(result);
        }
        return ruseltRsesultVo(result);
    }

    /**
     * 新增财务工资调整信息
     * @param financeAdjustment 财务工资调整信息
     * @return ResultVo
     */
    @Override
    public ResultVo addFinanceAdjustment(Finance_Adjustment financeAdjustment) {
        Integer result =lsFinanceMapper.addFinanceAdjustment(financeAdjustment);
        return ruseltRsesultVo(result);
    }

    /**
     * 查询今日财务信息
     * @param currentPage 当前页
     * @param limitPage   显示条数
     * @param startDate   开始日期
     * @param endDate     结束日期
     * @return 返回财务信息集
     */
    @Override
    public Q queryFinanceToday(Integer currentPage, Integer limitPage, Date startDate, Date endDate) {
        Integer param1=(currentPage-1)*limitPage;
        Integer param2=limitPage;
        List<Map> mapList=lsFinanceMapper.queryFinanceToday(param1,param2,startDate,endDate);
        Integer count=lsFinanceMapper.queryFinanceTodayCount(startDate,endDate);
        return resultQ(mapList,count);
    }

    /**
     * 每日对账查询
     * @param shopId 门店账号
     * @return 返回结果集
     */
    @Override
    public ResultVo queryFinanceDaily(Integer shopId) {
        List<Map> consumptionList= lsFinanceMapper.queryFinanceConsumption(shopId);
        List<Map> IndividualGuestList=lsFinanceMapper.queryFinanceIndividualGuest(shopId);
        List<Map> MemberList=lsFinanceMapper.queryFinanceMember(shopId);
        List<Map> StoreList=lsFinanceMapper.queryFinanceStore(shopId);
        List<Map> StoreDataList=lsFinanceMapper.queryFinanceStoreData(shopId);
        FinanceDaily financeDaily=new FinanceDaily();
        financeDaily.setConsumptionList(consumptionList);
        financeDaily.setIndividualGuestList(IndividualGuestList);
        financeDaily.setMemberList(MemberList);
        financeDaily.setStoreList(StoreList);
        financeDaily.setStoreDataList(StoreDataList);
        ResultVo resultVo=new ResultVo();
        if(financeDaily != null){
            resultVo.setCode(200);
            resultVo.setMsg("操作成功");
            resultVo.setData(financeDaily);
            return resultVo;
        }
        else {
            resultVo.setCode(500);
            resultVo.setMsg("系统出现错误");
            return resultVo;
        }
    }

    /**
     *
     * @param queryFinancePayment 日结记录查询实体
     * @return 日结记录查询
     */
    @Override
    public Q queryFinancePaymentList(QueryFinancePayment queryFinancePayment) {
        queryFinancePayment.setCurrentPage((queryFinancePayment.getCurrentPage()-1)*queryFinancePayment.getLimitPage());
        queryFinancePayment.setLimitPage(queryFinancePayment.getLimitPage());
        List<Map> mapList= lsFinanceMapper.queryFinancePaymentList(queryFinancePayment);
        Integer count = lsFinanceMapper.queryFinancePaymentListCount(queryFinancePayment);
        return resultQ(mapList,count);
    }

    @Override
    public ResultVo queryFinanceEmployee(Integer shopId) {
        List<Map> mapList=lsFinanceMapper.queryFinanceEmployee(shopId);
        return ruseltRsesultList(mapList);
    }

    /**
     *
     * @param search 搜索条件
     * @param currentPage 当前页
     * @param limitPage   显示条数
     * @param shopId
     * @return 日结记录搜索条件结果集
     */
    @Override
    public Q queryFinancePaymentListSearch(String search, Integer currentPage, Integer limitPage, Integer shopId) {
        Integer param1=(currentPage-1)*limitPage;
        Integer param2=limitPage;
        List<Map> mapList=lsFinanceMapper.queryFinancePaymentListSearch(search,param1,param2,shopId);
        Integer count=lsFinanceMapper.queryFinancePaymentListSearchCount(search,shopId);
        return resultQ(mapList,count);
    }

    /**
     * @param mapList 查询结果集
     * @return 返回查询结果集
     */
  public ResultVo ruseltRsesultList(List<Map> mapList){
      ResultVo resultVo=new ResultVo();
      if(!mapList.isEmpty() && mapList.size()>0){
          resultVo.setCode(200);
          resultVo.setMsg("操作成功");
          resultVo.setData(mapList);
          return resultVo;
      }else {
          resultVo.setCode(500);
          resultVo.setMsg("系统出现错误");
          return resultVo;
      }
  }

    /**
     * 返回结果类
     * @param result 操作结果
     * @return 返回结果
     */
    public ResultVo ruseltRsesultVo(Integer result){
        ResultVo resultVo=new ResultVo();
        if(result > 0){
            resultVo.setCode(200);
            resultVo.setMsg("操作成功");
            return resultVo;
        }else {
            resultVo.setCode(500);
            resultVo.setMsg("系统出现错误");
            return resultVo;
        }
    }


    /**
     *
     * @param mapList 查询结果集
     * @param count 查询结果集条数
     * @return 查询结果集返回
     */
    public Q resultQ(List<Map> mapList,Integer count){
        Q q=new Q();
        if(!mapList.isEmpty()&&mapList.size()>0){
            q.setCode(200);
            q.setMsg("操作成功");
            q.setCount(count);
            q.setData(mapList);
            return q;
        }else {
            q.setCode(500);
            q.setMsg("系统出现错误");
            return q;
        }
    }
}
